package com.zhiyou.crud.service.impl;

import com.zhiyou.crud.bean.Donate;
import com.zhiyou.crud.bean.Letter;
import com.zhiyou.crud.bean.Real;
import com.zhiyou.crud.bean.User;
import com.zhiyou.crud.dao.DonateMapper;
import com.zhiyou.crud.dao.LetterMapper;
import com.zhiyou.crud.dao.RealMapper;
import com.zhiyou.crud.dao.UserMapper;
import com.zhiyou.crud.service.Admin_UserManageService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: Admin_UserManageServiceImpl
 * @Author: ZengS
 * @Date: 2022/9/20 17:18
 */
@Service
public class Admin_UserManageServiceImpl implements Admin_UserManageService {

    @Resource
    UserMapper userMapper;
    @Resource
    DonateMapper donateMapper;
    @Resource
    RealMapper realMapper;
    @Resource
    LetterMapper letterMapper;

    /**
     * 根据给定参数模糊查询对应的编号或者名字
     * @param con
     * @return
     */
    @Override
    public List<User> getUsersByCon(String con) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("uno","%"+con+"%").orLike("uname","%"+con+"%");
        return userMapper.selectByExample(example);
    }

    @Override
    public List<User> getUsersMoney(String con) {
        List<User> users = getUsersByCon(con);
        for (User user : users) {
            Integer i = 0;//表示总金额
            Example example = new Example(Donate.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("uno",user.getUno());
            List<Donate> donates = donateMapper.selectByExample(example);
            for (Donate donate : donates) {
                i+=donate.getDmoney();
                user.setUmoney(i);
            }
        }
        return users;
    }

    @Override
    public List<Donate> getUserDonate(String uno) {
        Example example = new Example(Donate.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uno",uno);
        return donateMapper.selectByExample(example);
    }

    @Override
    public List<Real> getUserReal() {
        Example example = new Example(Real.class);
        example.setOrderByClause("status ASC");
        List<Real> reals = realMapper.selectByExample(example);
        reals.stream().forEach(real -> {
            real.setUser(userMapper.selectByPrimaryKey(real.getUno()));
        });
        return reals;
    }

    @Override
    public void updateNopassReal(Integer rno,Integer uno) {
        Real real = new Real();
        real.setRno(rno);
        real.setStatus(2);
        realMapper.updateByPrimaryKeySelective(real);
        User user = new User();
        user.setUno(uno);
        user.setRealstatus(2);
        userMapper.updateByPrimaryKeySelective(user);
        //加通知表
        Letter letter = new Letter();
        letter.setLrno(uno);
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        letter.setLtime(sdf.format(new Date()));
        letter.setLcontent("你的实名认证不通过");
        letterMapper.insertSelective(letter);
    }

    @Override
    public void updatePassReal(Integer rno,Integer uno) {
        //更新实名信息表
        Real real = new Real();
        real.setRno(rno);
        real.setStatus(3);
        realMapper.updateByPrimaryKeySelective(real);
        //更新user
        User user =new User();
        user.setUno(uno);
        user.setRealstatus(3);
        userMapper.updateByPrimaryKeySelective(user);
        //加通知表
        Letter letter = new Letter();
        letter.setLrno(uno);
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        letter.setLtime(sdf.format(new Date()));
        letter.setLcontent("你的实名认证已通过");
        letterMapper.insertSelective(letter);
    }

    @Override
    public void deleteUser(Integer uno) {
        userMapper.deleteByPrimaryKey(uno);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }
}
